//
//  LoginViewController.m
//  MyProject
//
//  Created by 石小强 on 2018/3/20.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "LoginViewController.h"
#import "PositionSelectViewController.h"
#import "NSUDManager.h"
#import "SelectRolesViewController.h"
#import <MJExtension.h>
#import "RolesViewController.h"
#import "WebViewController.h"
#import "AppDelegate.h"
#import <Hyphenate/Hyphenate.h>

@interface LoginViewController ()

/* 定时器 */
@property(strong,nonatomic) NSTimer *timer;
/* 定时多少秒 */
@property(assign,nonatomic) NSInteger count;

@end

@implementation LoginViewController {
    NSString *_code;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBezierPath *shadowPath = [UIBezierPath bezierPathWithRect:self.shadowV.bounds];
    self.shadowV.layer.shadowColor = [UIColor blackColor].CGColor;
    self.shadowV.layer.shadowOffset = CGSizeMake(0, 0);
    self.shadowV.layer.shadowOpacity = 0.2;
    self.shadowV.layer.shadowRadius = 10.f;
    self.shadowV.layer.shadowPath = shadowPath.CGPath;
}

- (IBAction)sendCodeBtnClick:(id)sender {
    if (!self.mobileT.text || self.mobileT.text.length == 0) {
        [CommonUtils showToast:@"请输入手机号"];
        return;
    }
    if (![Tool validMobile:self.mobileT.text]) {
        [CommonUtils showToast:@"请输入正确的手机号"];
        return;
    }
    
    if (![self.sendCodeB.titleLabel.text isEqualToString:@"发送验证码"]) {
        [CommonUtils showToast:@"验证码已发送"];
        return;
    }
    
    
    
    WS(weakSelf)
    [self showProgressWithView:self.view animated:YES];
    [SendRequest sendVerificationCodeWithTel:self.mobileT.text type:@"login" result:^(NSDictionary *result, NSError *error) {
        [weakSelf hideProgress:self.view animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            weakSelf.count = 59;
            [weakSelf.sendCodeB setTitle:[NSString stringWithFormat:@"%zds", self.count] forState:UIControlStateNormal];
            weakSelf.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeDown) userInfo: nil repeats:YES];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
        
    }];
}

- (void)timeDown {
    if (self.count != 1) {
        self.count -= 1;
        [self.sendCodeB setTitle:[NSString stringWithFormat:@"%zds", self.count] forState:UIControlStateNormal];
    } else {
        [self.sendCodeB setTitle:@"发送验证码" forState:UIControlStateNormal];
        [self.timer invalidate];
        self.timer = nil;
    }
}

- (IBAction)loginBtnClick:(id)sender {
    
//    [SendRequest getIndustriesWithResult:^(NSDictionary *result, NSError *error) {
//        result = [CommonUtils decryptDicWithResult:result];
//        NSLog(@"result = %@", result);
//    }];
    
//    [SendRequest getRegionWithResult:^(NSDictionary *result, NSError *error) {
//        result = [CommonUtils decryptDicWithResult:result];
//        NSLog(@"result = %@", result);
//    }];
    
//    [SendRequest getJobListWithIndustryId:@"" andJobPid:@"" result:^(NSDictionary *result, NSError *error) {
//        result = [CommonUtils decryptDicWithResult:result];
//        NSLog(@"result = %@", result);
//    }];
//    
//    return;
//    SelectRolesViewController *srVC = [[SelectRolesViewController alloc] init];
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:srVC];
//    [self presentViewController:nav animated:YES completion:nil];
//
//
//    return;
    
    if (!self.mobileT.text || self.mobileT.text.length == 0) {
        [CommonUtils showToast:@"请输入手机号"];
        return;
    }
    if (![Tool validMobile:self.mobileT.text]) {
        [CommonUtils showToast:@"请输入正确的手机号"];
        return;
    }
    if (!self.codeT.text || self.codeT.text.length == 0) {
        [CommonUtils showToast:@"请输入验证码"];
        return;
    }
    
    WS(weakSelf)
    [self showProgressWithView:self.view animated:YES];
    [SendRequest loginWithTel:self.mobileT.text smsCode:self.codeT.text result:^(NSDictionary *result, NSError *error) {
        [weakSelf hideProgress:self.view animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            /**登录成功*/
            UserModel *user = [UserModel mj_objectWithKeyValues:[result objectForKey:@"data"]];
            SaintiLog(@"登录成功后数据 = %@", [result objectForKey:@"data"]);
            [NSUDManager setCurrentUser:user];
            
            if ([user.role isEqualToString:@"0"] || [user.role integerValue] == 0) {
                RolesViewController *srVC = [[RolesViewController alloc] init];
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:srVC];
                [self presentViewController:nav animated:YES completion:nil];
            } else {
                [((AppDelegate *)[UIApplication sharedApplication].delegate) gotoHome];

            }
            if(![[EMClient sharedClient] isConnected]){
                [[EMClient sharedClient] loginWithUsername:user.userId
                                                  password:user.userId
                                                completion:^(NSString *aUsername, EMError *aError) {
                                                    if (!aError) {
                                                        NSLog(@"登录成功");
                                                    } else {
                                                        NSLog(@"登录失败");
                                                    }
                                                }];
            }
            
               
                
        } else {
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
    
    
    
    
//    [SendRequest getRegionWithResult:^(NSDictionary *result, NSError *error) {
//        [weakSelf hideProgress:self.view animated:YES];
//        result = [CommonUtils decryptDicWithResult:result];
//        if([result[@"result"] integerValue] == 1) {
//            SaintiLog(@"返回值 = %@", result);
//        } else {
//            [CommonUtils showToast:[result objectForKey:@"msg"]];
//        }
//
//    }];
}

- (IBAction)protocolBtnClick:(id)sender {
    WebViewController *vc = [[WebViewController alloc]init];
    vc.titleStr =@"用户协议";
    vc.url = [NSString stringWithFormat:@"%@%@",ImageBaseURL,@"台阶网用户协议.docx"];
    vc.isWord = YES;
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:nil];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSLog(@"textField.text = %@", textField.text);
    
    if (range.location == 11 && range.length == 0 && string.length == 1) {
        [CommonUtils showToast:@"最多输入11位手机号"];
        return NO;
    }
    
    return YES;
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

@end
